function GrabzIt(key)
{
	return new (function(key)
	{	
		this.key = key;
		
		this.Create = function(url, options)
		{
			var protocol = '//';
			if (window.location.protocol != 'https' && window.location.protocol != 'http')
			{
				protocol = 'http://';
			}

			var grabzItUrl = protocol + 'api.grabz.it/services/javascript.ashx?key='+encodeURIComponent(this.key)+'&url=' + encodeURIComponent(url);

			for(var k in options)
			{
				if (k != 'format' && k != 'cache' && k != 'customWaterMarkId' && k != 'quality'
				&& k != 'country' && k != 'filename' && k != 'errorid' && k != 'errorclass' &&
				k != 'onfinish' && k != 'onerror' && k != 'delay' && k != 'bwidth' && k != 'bheight' &&
				k != 'height' && k != 'width' && k != 'target' && k != 'requestas' && k != 'download' && k != 'suppresserrors' && k != 'displayid' && k != 'displayclass' && k != 'background' && k != 'pagesize' && k != 'orientation' && k != 'includelinks' && k != 'includeoutline' && k != 'title' && k != 'coverurl' && k != 'mtop' && k != 'mleft' && k != 'mbottom' && k != 'mright' && k != 'tabletoinclude' && k != 'includeheadernames' && k != 'includealltables' && k != 'start' && k != 'duration' && k != 'speed' && k != 'fps' && k != 'repeat' && k != 'reverse')
				{
					throw "Option " + k + " not recognized!";
				}
				
				var v = options[k];
				if (v != null)
                		{
					grabzItUrl += '&' + k + '=' + encodeURIComponent(v);
				}
			}

			var scriptNode = document.createElement('script');
			scriptNode.src = grabzItUrl;

			return scriptNode;
		};		

		this.AddTo = function(container, url, options)
		{
			var elem = null;
			if (typeof container == 'string' || container instanceof String)
			{
				elem = document.getElementById(container);
				if (elem == null)
				{
					throw "An element with the id " + container + " was not found";
				}
			}
			else if (container.nodeType === 1)
			{
				elem = container;
			}

			if (elem == null)
			{
				throw "No valid element was provided to attach the screenshot to";
			}

			elem.appendChild(this.Create(url, options));
		}
	})(key);
}

function GrabzItPreviewFinished(id)
{
	var obj = document.getElementById("grabzit-screenshot-result");

	if (obj != null)
	{
		obj.removeAttribute("id");
		var children = obj.parentNode.parentNode.childNodes;

		for (var j = 0; j < children.length; j++)
		{
			if (children[j].className == "grabzit-preview-loader")
			{
				children[j].style.display = "none";
			}
			if (children[j].className == "grabzit-preview-screenshot-frame")
			{
				children[j].style.display = "block";
			}				
		}	
	}
}

function GrabzItPreview(key, options)
{
	this._guid = function()
	{
    	return this._s4() + this._s4() + '-' + this._s4() + '-' + this._s4() + '-' + this._s4() + '-' + this._s4() + this._s4() + this._s4();
	}

	this._s4 = function() 
	{
    	return Math.floor((1 + Math.random()) * 0x10000).toString(16).substring(1);
	}
	
	this._getJSFunction = function(jsAttribute)
	{
		if (jsAttribute != null)
		{
			return jsAttribute + ";";
		}
		return "";
	}

	var __construct = function(that) 
	{	
		var width = 197;
		var height = 154;

		if (options != null)
		{
			if (options.width != null)
			{
				width = options.width;
			}
			if (options.height != null)
			{
				height = options.height;
			}
		}
		else		
		{
			options = new Array();
		}
		
		var links = document.getElementsByClassName("grabzit-preview");

		for (var i = 0; i < links.length; i++)
		{
			var link = links[i];
			var href = link.getAttribute("href");
			if (link.getAttribute("grabzit-href") != null)
			{
				href = link.getAttribute("grabzit-href");
			}	
			var captionText = link.getAttribute("title");
			var guid = that._guid();
			
			var rect = link.getBoundingClientRect()
			var pos = rect.left;
			var leftOrRight = "left";
			
			if ((pos + width + 50) > window.innerWidth)
			{
				leftOrRight = "right";
				pos = 10;
			}
			
			var div = document.createElement('div');
			div.setAttribute("style","display:none;z-index:100000;position:fixed;"+leftOrRight+":"+pos+"px;");
			div.id = guid;
			div.className = "grabzit-preview-container";

			var divLoading = document.createElement('div');
			divLoading.setAttribute("style","width:"+width+"px;height:"+height+"px;");
			divLoading.className = "grabzit-preview-loader";

			var img = document.createElement('img');
			img.src = "";
			
			img.setAttribute("style","width:66px;height:66px;margin-left:"+(Math.round(width/2)-(66/2))+"px;margin-top:"+(Math.round(height/2)-(66/2))+"px;");

			divLoading.appendChild(img);

			div.appendChild(divLoading);

			var divFrame = document.createElement('div');
			divFrame.setAttribute("style","display:none;width:"+width+"px;height:"+height+"px;");
			divFrame.className = "grabzit-preview-screenshot-frame";
			
			options['width'] = width;
			options['height'] = height;
			options['displayid'] = 'grabzit-screenshot-result';
			options['errorid'] = 'grabzit-screenshot-result';
			options['errorclass'] = 'grabzit-preview-error';
			options['displayclass'] = 'grabzit-preview-screenshot';
			options['onfinish'] = 'GrabzItPreviewFinished';
			options['onerror'] = 'GrabzItPreviewFinished';
			
			try
			{
				GrabzIt(key).AddTo(divFrame, href, options);			
			}
			catch(e)			
			{
				alert(e);
			}
			
			div.appendChild(divFrame);			

			var divCaption = document.createElement('div');
			divCaption.innerHTML = captionText;
			divCaption.className = "grabzit-preview-caption";
			div.appendChild(divCaption);

			link.parentNode.insertBefore(div, link.nextSibling);
			link.setAttribute('onmouseout', that._getJSFunction(link.getAttribute('onmouseout')) + "document.getElementById('"+guid+"').style.display='none';");
			link.setAttribute('onmouseover', that._getJSFunction(link.getAttribute('onmouseover')) + "document.getElementById('"+guid+"').style.display='block';");
		}
	}(this)
}